package com.runen.wnhz.runen.presenter.iPresenter;

import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.BaseEntity;
import com.runen.wnhz.runen.data.entity.BaseUnified;
import com.runen.wnhz.runen.data.entity.MineBean;
import com.runen.wnhz.runen.data.entity.MyOrderListBean;
import com.runen.wnhz.runen.presenter.BasePresenter;
import com.runen.wnhz.runen.presenter.Contart.MyOrderContract;
import com.runen.wnhz.runen.presenter.model.MyOrderModel;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 描述：
 * 作者： 天天童话丶
 * 时间： 2018/6/1.
 */

public class IMyOrderPresenter extends BasePresenter<MyOrderModel, MyOrderContract.View> {

    @Inject
    public IMyOrderPresenter(MyOrderModel mModel, MyOrderContract.View mView) {
        super(mModel, mView);
    }

    /**
     * 获取订单列表
     */
    public void getMyOrderList(){
        Map<String,String> map = new HashMap<>();
        map.put("token", mView.getToken());
        map.put("page", mView.getPage());

//        log(map);

        mModel.getMyOrderList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<MyOrderListBean>>() {
                    @Override
                    public void call(BaseEntity<MyOrderListBean> entity) {
                        switch (entity.getRe()){
                            case -1:
                                mView.onTokenError(entity.getInfo());
                                break;
                            case 0:
                                mView.onError(entity.getInfo());
                                break;
                            case 1:
                                mView.onSuccess(entity.getData());
                                break;
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.onError(throwable.getMessage().toString());
                    }
                });
    }

    /**
     * 取消订单
     */
    public void cancelOrder(){
        Map<String,String> map = new HashMap<>();
        map.put("token", mView.getToken());
        map.put("order_id", mView.getOrderId());

//        log(map);

        mModel.cancelOrder(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseUnified>() {
                    @Override
                    public void call(BaseUnified entity) {
                        switch (entity.getRe()){
                            case "-1":
                                mView.onTokenError(entity.getInfo());
                                break;
                            case "0":
                                mView.onError(entity.getInfo());
                                break;
                            case "1":
                                mView.onSuccess();
                                break;
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.onError(throwable.getMessage().toString());
                    }
                });
    }
}
